package com.liyuhan.onlinetaxibackground.Service.impl;

import com.liyuhan.onlinetaxibackground.POJO.Car;
import com.liyuhan.onlinetaxibackground.POJO.Driver;
import com.liyuhan.onlinetaxibackground.Repository.DriverRepository;
import com.liyuhan.onlinetaxibackground.Repository.UserRepository;
import com.liyuhan.onlinetaxibackground.Service.DriverService;
import com.liyuhan.onlinetaxibackground.Util.GetRequestBody;
import com.liyuhan.onlinetaxibackground.dto.DataRequest;
import com.liyuhan.onlinetaxibackground.dto.DataResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DriverImpl implements DriverService {
    @Autowired
    private DriverRepository driverRepository;

    @Autowired
    private UserRepository userRepository;

    private Map<String, String> map = new HashMap<>();

    @Override
    public void saveLocation(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String phone = requestBody.getData().get("phone").toString();
        String city = requestBody.getString("city");
        String longitude = requestBody.getString("longitude");
        String latitude = requestBody.getString("latitude");
        String address = requestBody.getString("address");

        // 获取司机
        Driver driver = driverRepository.findDriverByUser(userRepository.findUserByPhone(phone).get()).get();

        // 设置司机信息
        driver.setCity(city);
        driver.setLongitude(Double.valueOf(longitude));
        driver.setLatitude(Double.valueOf(latitude));
        driver.setAddress(address);

        // 保存司机
        driverRepository.save(driver);

    }

    @Override
    public void changeGetOrderState(String request) {
        // 修改司机接单状态
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String phone = requestBody.getData().get("phone").toString();
        String state = requestBody.getData().get("state").toString();

        // 获取司机
        Driver driver = driverRepository.findDriverByUser(userRepository.findUserByPhone(phone).get()).get();

        // 设置司机信息
        driver.setState(state);

        // 保存司机
        driverRepository.save(driver);

    }

    @Override
    public DataResponse getCarInfo(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String phone = requestBody.getData().get("phone").toString();

        Driver driver = driverRepository.findDriverByUser(userRepository.findUserByPhone(phone).get()).get();

        Car car = driver.getCar();

        map.clear();
        map.put("carNumber", car.getCarNumber());
        map.put("carBrand", car.getCarBrand());
        map.put("carColor", car.getCarColor());

        return new DataResponse("200", map);
    }

    @Override
    public DataResponse saveCarInfo(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String phone = requestBody.getData().get("phone").toString();
        String carNumber = requestBody.getData().get("carNumber").toString();
        String carColor = requestBody.getData().get("carColor").toString();
        String carBrand = requestBody.getData().get("carBrand").toString();

        Driver driver = driverRepository.findDriverByUser(userRepository.findUserByPhone(phone).get()).get();

        Car car = driver.getCar();
        car.setCarNumber(carNumber);
        car.setCarBrand(carBrand);
        car.setCarColor(carColor);

        driverRepository.save(driver);

        return new DataResponse("200", null);
    }
}
